home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / alpha.arc / TNSERV.C < prev    next >
C/C++ Source or Header  |  1988-03-06  |  3KB  |  107 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "mbuf.h"
  4. #include "timer.h"
  5. #include "icmp.h"
  6. #include "netuser.h"
  7. #include "tcp.h"
  8. #include "telnet.h"
  9. #include "session.h"
  10.  
  11. struct tcb *tnet_tcb;
  12. tn1(argc,argv)
  13. char *argv[];
  14. {
  15.     struct socket lsocket;
  16.     extern int32 ip_addr;
  17.     void tnet_state();
  18.     void t_state(),rcv_char();
  19.  
  20.     /* Incoming Telnet */
  21.     lsocket.address = ip_addr;
  22.     if(argc < 2)
  23.         lsocket.port = TELNET_PORT;
  24.     else
  25.         lsocket.port = atoi(argv[1]);
  26.     tnet_tcb = open_tcp(&lsocket,NULLSOCK,TCP_SERVER,0,rcv_char,NULLVFP,tnet_state,0,(char *)NULL);
  27. }
  28. /* Handle incoming Telnet connect requests by creating a Telnet session,
  29.  * then change upcall vector so it behaves like an ordinary Telnet session.
  30.  * 
  31.  */
  32. static void
  33. tnet_state(tcb,old,new)
  34. struct tcb *tcb;
  35. char old,new;
  36. {
  37.     struct telnet *tn;
  38.     struct session *s,*newsession();
  39.     void t_state();
  40.     char *a;
  41.  
  42.     switch(new){
  43.     case ESTABLISHED:
  44.         log(tcb,"open Telnet");
  45.         /* Allocate a session descriptor */
  46.         if((s = newsession()) == NULLSESSION){
  47.             printf("\007Incoming Telnet call from %s refused; too many sessions\n",
  48.              psocket(&tcb->conn.remote));
  49.             fflush(stdout);
  50.             sndmsg(tcb,"Call rejected; too many sessions on remote system\n");
  51.             close_tcp(tcb);
  52.             return;
  53.         }
  54.         a = inet_ntoa(tcb->conn.remote.address);
  55.         if((s->name = malloc((unsigned)strlen(a)+1)) != NULLCHAR)
  56.             strcpy(s->name,a);
  57.         s->type = TELNET;
  58.         s->parse = send_tel;
  59.         /* Create and initialize a Telnet protocol descriptor */
  60.         if((tn = (struct telnet *)calloc(1,sizeof(struct telnet))) == NULLTN){
  61.             printf("\007Incoming Telnet call refused; no space\n");
  62.             fflush(stdout);
  63.             sndmsg(tcb,"Call rejected; no space on remote system\n");
  64.             close_tcp(tcb);
  65.             s->type = FREE;
  66.             return;
  67.         }
  68.         tn->session = s;    /* Upward pointer */
  69.         tn->state = TS_DATA;
  70.         s->cb.telnet = tn;    /* Downward pointer */
  71.  
  72.         tcb->user = (char *)tn;    /* Upward pointer */
  73.         tn->tcb = tcb;        /* Downward pointer */
  74.         printf("\007Incoming Telnet session %lu from %s\n",
  75.          (long)(s - sessions),psocket(&tcb->conn.remote));
  76.         fflush(stdout);
  77.         tcb->s_upcall = t_state;
  78.         return;
  79.     case CLOSED:
  80.         /* This will only happen if the connection closed before
  81.          * the session was set up, e.g., if we refused it because
  82.          * there were too many sessions, or if the server is being
  83.          * shut down.
  84.          */
  85.         if(tcb == tnet_tcb)
  86.             tnet_tcb = NULLTCB;
  87.         del_tcp(tcb);
  88.         break;
  89.     }
  90. }
  91. /* Shut down Telnet server */
  92. tn0()
  93. {
  94.     if(tnet_tcb != NULLTCB)
  95.         close_tcp(tnet_tcb);
  96. }
  97. static
  98. sndmsg(tcb,msg)
  99. struct tcb *tcb;
  100. char *msg;
  101. {
  102.     struct mbuf *bp;
  103.  
  104.     bp = qdata(msg,(int16)strlen(msg));
  105.     send_tcp(tcb,bp);
  106. }
  107.